<template>
  <div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FormPanel
      ref="baseInfo"
      :list="initFormBase"
      :cols="2"
      :labelWidth="140"
      :isSubmitBtn="false"
      :formType="handleType"
    />
    <div class="dialogBottmView">
      <el-button @click="closeDialogTop">返 回</el-button>
      <el-button v-if="handleType != 'show'" @click="saveDialog()" :loading="saveLoading" type="primary">
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
import {dictionary} from '@/utils/dictMixin';
import {postSaveBatteryPlant, putBatteryPlant} from '@/api/partsmanage/batteryTrace/batteryPlant';
import {cslxArr, sfqyArr} from './constant';

export default {
  components: {},
  props: {
    handleType: {
      type: String,
      default: 'add'
    },
    pageType: {
      type: String,
      default: ''
    },
    value: {
      type: Object,
      default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      formData: {},
      initFormBase: this.initFormBaseFun(),
      saveLoading: false,
    };
  },
  mounted() {
    //判断是新增，还是修改
    if (this.handleType == 'add') {
    } else {
      this.formData = Object.assign({}, this.value.data);
      this.init();
      // getBatteryPlantById(this.value.id).then(res => {
      //   if (res.resultCode === 200) {
      //     const data = Object.assign({}, this.formData, this.value.data, res.data);
      //     console.info(data)
      //     this.formData = data;
      //
      //   }
      // })
    }
  },
  computed: {
    culFormData() {
      return this.formData
    }
  },
  methods: {
    //修改时，初始化页面数据
    init() {
      //循环为表单赋值
      this.initFormBase.forEach(x => {
        x.initialValue = this.culFormData[x.fieldName];
      });
    },
    //基本表单信息
    initFormBaseFun() {
      return [
        {
          type: 'SELECT',
          label: '厂商类型',
          fieldName: 'type',
          placeholder: '请选择',
          initialValue: '',
          itemList: cslxArr,
          rules: [
            {required: true, message: '请选择', trigger: 'change'},
          ]
        },
        {
          type: 'INPUT',
          label: '厂商名称',
          fieldName: 'name',
          placeholder: '请选择',
          initialValue: '',
          rules: [
            {required: true, trigger: 'change', message: '请选择'},
          ]
        },
        {
          type: 'INPUT',
          label: '社会信用代码',
          fieldName: 'socialCreditCode',
          placeholder: '请输入',
          initialValue: '',
          rules: [
            {required: true, message: '请选择', trigger: 'change'},
          ]
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'available',
          placeholder: '请选择',
          initialValue: '',
          itemList: sfqyArr,
          rules: [
            {required: true, message: '请选择', trigger: 'change'},
          ]
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入',
          rules: [
            {required: false, trigger: 'change'}
          ],
          initialValue: '',
          maxlength: 300,
          selfCols: 2
        },
      ];
    },
    //保存
    async saveDialog(xsddzt = 'C') {
      this.saveLoading = true;
      if (this.handleType == 'show' && xsddzt === 'T') {
        return this.tbHandle()
      }
      let [err, data] = await this.$refs.baseInfo.GET_FORM_DATA();
      if (err) {
      } else {
        this.formData = Object.assign({}, this.formData, data);
        console.info(`this.culFormData`)
        console.info(this.culFormData)
        const formData = {
          ...this.culFormData,
          xsddzt
        }
        let res;
        if (this.handleType === "add") { // 新增
          res = await postSaveBatteryPlant(formData);
        } else { // 修改
          res = await putBatteryPlant(formData);
        }
        if (res.resultCode === 200) {
          this.$notify.success({title: '提示信息', message: xsddzt == 'T' ? '提报成功' : '保存成功!'});
          // 重新加载
          this.closeDialog('refresh');
        }
      }
      this.saveLoading = false;
    },
    // 提报
    async tbHandle() {
      this.saveLoading = true
      const res = await postUpdateStatus({
        id: this.formData.id,
        xsddzt: 'T'
      })
      if (res.resultCode == 200) {
        this.$notify.success({title: '提示信息', message: '提报成功'});
        this.closeDialog('refresh');
      }
      this.saveLoading = false
    },
    closeDialogTop() {
      return this.closeDialog('closeDialog');
      this.$confirm('确认关闭窗口吗？您所做的操作将不会被保存！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
        })
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', type);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
